package com.cgwx.service.impl;

import com.cgwx.entity.Notification;
import com.cgwx.mapper.NotificationMapper;
import com.cgwx.service.INotificationService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class NotificationServiceImpl implements INotificationService {
    @Resource
    private NotificationMapper notificationMapper;

    private final Object lock = new Object();
    @Override
    public List<Notification> getAllNotification() {
        return notificationMapper.getAllNotification();
    }

    @Override
    public List<Notification> getNotification(Notification notification) {
        return notificationMapper.getNotification(notification);
    }

    @Override
    public String deleteNotificationById(String id) {
        boolean result = notificationMapper.deleteNotificationById(id);
        if(result) return "删除成功!";
        else return "删除失败!";
    }

    @Override
    public synchronized String insertNotification(Notification notification) {
        if(notification.getAccount() == null || notification.getMessage() == null) return "参数不全!";
        Timestamp time = (Timestamp.valueOf(LocalDateTime.now()));
        notification.setTime(time);
        notification.setId(generateId());
        boolean result = notificationMapper.insertNotification(notification);
        if(result) return "新增成功!";
        else return "新增失败!";
    }
    public String generateId() {
        return UUID.randomUUID().toString();
    }
}
